package com.aaa.util;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import com.aaa.entity.MyReturn;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ：admin
 * @date ：Created in 2022/6/2 11:08
 * @description：
 * @modified By：
 */
public class MyTools {
    /**
        * @Description: 拼接sql语句
        * @Param: [oldSql, map, countFlag]
        * @return: java.lang.String
    */
    public static String makeSql(String oldSql, Map<String,Object> map,boolean countFlag){
        StringBuffer sql = new StringBuffer(oldSql);
        if (map != null || map.size() > 0){

            //获取所有的查询条件
            Map<String, String> whereMap = new HashMap<>();
            for (String key : map.keySet()) {
                if (key.contains("search")) {
                    //去掉前缀的search标记，方便sql语句查询
                    whereMap.put(key.replace("search", ""), map.get(key) + "");
                }
            }
            //拼接where查询条件
            for (Map.Entry<String, String> entry : whereMap.entrySet()) {
                sql.append(" and " + entry.getKey() + " like " + " '%" + entry.getValue() + "%' ");
            }
            //判断是否排序
            if (ObjectUtil.isNotEmpty(map.get("field"))){
                String field = map.get("field").toString();
                String order = map.get("order").toString();
                sql.append(" order by ").append(field).append(" ").append(order);
            }
            if (!countFlag){
                //判断是否分页
                if (ObjectUtil.isNotEmpty(map.get("page"))){
                    Integer page = Integer.parseInt(map.get("page").toString());
                    Integer limit = Integer.parseInt(map.get("limit").toString());
                    sql.append(" limit ").append((page - 1) * limit).append(",").append(limit);
                }
            }
        }
        return sql.toString();
    }

    /** 
        * @Description: 传入一个原始返回对象，得到一个成功的返回对象
        * @Param: [myReturn]
        * @return: com.aaa.entity.LayuiTable
    */
    public  static MyReturn getSuccessResult(MyReturn myReturn){
        myReturn.setCode(RetStatusEnum.SUCCESS.getCode());
        myReturn.setMsg(RetStatusEnum.SUCCESS.getMessage());
        return  myReturn;
    }
    /** 
        * @Description: 传入一个原始返回对象，得到一个失败的返回对象
        * @Param: [myReturn]
        * @return: com.aaa.entity.LayuiTable
    */
    public static MyReturn getFailResult(MyReturn myReturn){
        myReturn.setCode(RetStatusEnum.FAIL.getCode());
        myReturn.setMsg(RetStatusEnum.FAIL.getMessage());
        return  myReturn;
    }

    /**
     * @Description:密码加盐加密
     * @Param: [password]   (明文密码)
     * @return: java.util.Map<java.lang.String,java.lang.String>    (返回封装了加密之后的密码和盐值)
     */
    public static Map<String, String> makePasswordSalt(String password) {

        Map<String, String> map = new HashMap<>();

        //随机生成密钥(盐值)
        byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
        //将盐值转换格式（方便数据库保存）,byte数组转字符串
        String salt = Base64.encode(key);

        //构建加密对象
        SymmetricCrypto aes = null;
            //Base64.decode(salt)，将字符串转换成byte数组
            aes = new SymmetricCrypto(SymmetricAlgorithm.AES, Base64.decode(salt));

        //加密为16进制表示
        String encryptHex = aes.encryptHex(password);
        //加密之后的密文
        map.put("passwordNew", encryptHex);
        //盐值，保存盐值的作用是为了登录
        map.put("salt", salt);
        return map;
    }

    /**
     * @Description:密码加盐加密
     * @Param: [password, salt] (明文密码、盐值)
     * @return: java.lang.String （密文）
     */
    public static String makePasswordSalt(String password,String salt){
        //构建
        SymmetricCrypto aes = null;
            aes = new SymmetricCrypto(SymmetricAlgorithm.AES, Base64.decode(salt));

        //加密为16进制表示
        String encryptHex = aes.encryptHex(password);
        return encryptHex;
    }
}
